#' Data pre-processing of the enzyme rock1 
#' IC50 bioactivities from the ChEMBL database 


# Workspace and History ----------
save.image("rock1_preprocess.Rdata")
savehistory("rock1_preprocess.Rhistory")


# Libraries ---------- 
library(tidyverse)
library(dplyr)
library(skimr)
library(recipes)
library(janitor)


# Raw dataset --------------------------------------------------
raw_df <- read.csv2("raw_chembl_rock1_ic50.csv",
                    sep = ";",
                    header = TRUE)  
raw_df[raw_df == ""] <- NA
sum(is.na(raw_df)) 
sum(duplicated(raw_df$Molecule.ChEMBL.ID)) 
screened_df <- distinct(raw_df, Molecule.ChEMBL.ID,
                        .keep_all = TRUE) 
sum(duplicated(screened_df$Molecule.ChEMBL.ID)) 

# Bioactivity classification ---------------------------------------------

# Analyzing pIC50 values for 
# bioactivity classification ranges.

# What ranges constitute 
# an active and an inactive compound?

# Active compounds --------------------------------------------------
active <- screened_df %>%
  select(Molecule.ChEMBL.ID,
         Smiles,
         Standard.Relation,
         Standard.Value,
         Document.ChEMBL.ID) %>% 
  mutate(Molar = as.numeric(Standard.Value) * 10 ^ -9) %>%
  mutate(pIC50 = -log10(Molar)) %>%
  filter(pIC50 >= 5.4) %>% # 5.4 is the pIC50 of velpatasvir
  arrange(pIC50) %>%
  na.omit()

# Inactive compounds --------------------------------------------------
inactive <- screened_df %>%
  select(Molecule.ChEMBL.ID,
         Smiles,
         Standard.Relation,
         Standard.Value,
         Document.ChEMBL.ID) %>%
  mutate(Molar = as.numeric(Standard.Value) * 10 ^ -9) %>%
  mutate(pIC50 = -log10(Molar)) %>%
  filter(pIC50 < 5.4) %>% 
  arrange(pIC50) %>%
  na.omit()

# Intermediate Compounds --------------------------------------------- 

# What ranges constitute 
# an intermediate compound?

# Analyzing the box and whisker plot 
# of the active and inactive
# using their numerical quartile values.

# Boxplot --------------------------------------------------
boxplot(active$pIC50, inactive$pIC50,
        xlab = "Bioactivity class",
        ylab = "pIC50 values",
        names = c("Active", "Inactive")) 

active_skim <- skim(active$pIC50) # p0 for intermediate upper limit
inactive_skim <- skim(inactive$pIC50) # p50 for lower limit

# Intermediate compounds -----------------------------------
intermediate <- screened_df %>%
  select(Molecule.ChEMBL.ID,
         Smiles,
         Standard.Relation,
         Standard.Value,
         Document.ChEMBL.ID) %>%
  mutate(Molar = as.numeric(Standard.Value) * 10 ^ -9) %>%
  mutate(pIC50 = -log10(Molar)) %>%
  filter(between(pIC50, 5, 5.39)) %>% # 5.20 < pIC50 > 5.39
  arrange(pIC50) %>%
  na.omit()

# All classes ------------------------------ 
boxplot(active$pIC50, 
        intermediate$pIC50,
        inactive$pIC50,
        xlab = "Bioactivity class",
        ylab = "pIC50 values",
        names = c("active", "intermediate", "inactive"))


cmpds_all <-
  rbind(active, 
        intermediate, 
        inactive) 

# Duplicate compounds and SMILES -------------------------
sum(duplicated(cmpds_all$Molecule.ChEMBL.ID))
sum(duplicated(cmpds_all$Smiles))

# Final dataset cleaning -----------------------------------
cmpds_all_final <-
  distinct(cmpds_all, 
           Molecule.ChEMBL.ID,
           .keep_all = TRUE)
cmpds_all_final <- distinct(cmpds_all,
                            Smiles,
                            .keep_all = TRUE)

# Classes categorical ----------------------------------------
cmpds_all_final$Bioactivity.Class <-
  as.factor(ifelse(
    cmpds_all_final$pIC50 >= 5.4,
    "active",
    ifelse(cmpds_all_final$pIC50 <= 4.99, "inactive",
           "intermediate")
  ))
summary.factor(cmpds_all_final$Bioactivity.Class) 
final_df <-
  arrange(cmpds_all_final, pIC50, by_group = TRUE) # increasing pIC50


# Data Visualization ---------------------------------------------
final_df %>%
  ggplot(aes(Bioactivity.Class)) +
  geom_bar(fill = "#3983C4") +
  #coord_flip() + # flipping/inversing the coordinate
  theme_bw() +
  labs(x = "Bioactivity Class",
       y = "Frequency",
       title = "Distribution of Bioactivities")
#fct_infreq(Bioactivity.Class) # create/arrange in order

# PNG export ------------------------------
ggsave(
  "rock1_bar_chart.png",
  width = 10,
  height = 7,
  units = "cm",
  dpi = 300
)

# Final dataset csv export --------------------
write.csv(final_df, file =
            "rock1_screened.csv")